package com.ghxxzx.daglcustom.controller.model;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;

@Controller
@RequestMapping("/model")
public class ActivitiModelImageController {

	@Autowired
	private RepositoryService repositoryService;

	/**
	 * 获取图片
	 */
	@RequestMapping("/getImgBase64")
	@ResponseBody
	public String getImgBase64(String modelId) {
		// 模型模块
		byte[] bytes = repositoryService.getModelEditorSourceExtra(modelId);
		String image = Base64.getEncoder().encodeToString(bytes);
		return "data:image/png;base64," + image;

	}

	/**
	 * 获取图片
	 * 
	 * @throws IOException
	 */
	@RequestMapping("/getImg")
	public void getImg(String modelId, HttpServletResponse response) throws IOException {
		response.setContentType("image/jpg"); // 设置返回的文件类型

		// 模型模块
		byte[] bytes = repositoryService.getModelEditorSourceExtra(modelId);

		OutputStream os = response.getOutputStream();
		os.write(bytes);
		os.flush();
		os.close();
	}

	/**
	 * 获取图片
	 * 
	 * @throws IOException
	 */
	@RequestMapping("/getImgByDeploymentId")
	public void getImgByDeploymentId(String deploymentId, HttpServletResponse response) throws IOException {
		Model model = repositoryService.createModelQuery().deploymentId(deploymentId).singleResult();

		String modelId = model.getId();
		getImg(modelId, response);
	}

}
